var app = getApp();
var bmap = require('../budu-map/bmap-wx.min.js');
var wxMarkerData = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    'address': '定位中',
    ak: "AXMRrsEZ0CGfogyRENeexOTkHxauhZtz",   //填写申请到的ak
    tabNum:0,
    tabone:false,
    lisHidden:false,
    tabfixed:false,
    shut:false,
    tabtwo:false,
    tabthree: false,
    tabfour: false,
    tabfixed: false,
    lisHidden: true,
    lisNum01: 33,
    lisNum02: 22,
    lisNum03: 22,
    lisNum04: 22,
    valueOne: '全部品牌',
    valueTwo: '全部规格',
    valueThree: '全部花纹',
    valueFour:'综合',
    tabValue:[
      '价格由高到低',
      '价格由低到高'
    ],
    luntaione: '全部品牌',
    luntaitwo: '全部规格',
    luntaithree: '全部花纹',
    luntaifour: '综合',
    lists:[],
    gueige:[],
    huawen:[],
    brand:[],
    search:'',
    bid:0,
    gg:'',
    hw:'',
    jg:''
  },
  queding (e) {
    this.setData({
      valueOne: this.data.luntaione,
      valueTwo: this.data.luntaitwo,
      valueThree: this.data.luntaithree,
      valueFour: this.data.luntaifour,
      tabone: false,
      tabtwo: false,
      tabthree: false,
      tabfour: false,
      tabfixed: false
    });
    this.loadLists();
  },
  lisClick04(e) {
    this.setData({
      lisHidden: true,
      lisNum04: e.target.dataset.index,
      shut: true,
      luntaifour: e.target.dataset.value,
      jg: e.target.dataset.value,
    })
  },
  lisClick03(e) {
    this.setData({
      lisHidden: true,
      lisNum03: e.target.dataset.index,
      shut: true,
      luntaithree: e.target.dataset.value,
      hw: e.target.dataset.value
    })
  },
  lisClick02(e) {
    
    this.setData({
      lisHidden: true,
      lisNum02: e.target.dataset.index,
      shut: true,
      luntaitwo: e.target.dataset.value,
      gg: e.target.dataset.value
    })
  },
  lisClick01(e) {
    console.log(e)
    var bid = e.currentTarget.dataset.bid;
    this.setData({
      lisHidden: true,
      lisNum01: e.target.dataset.index,
      shut:true,
      luntaione:e.target.dataset.value,
      bid:bid
    });
    
  },
  showFixed: function (e) {
    var that = this;
    that.setData({
      tabone: false,
      tabtwo: false,
      tabthree: false,
      tabfour: false,
      tabfixed: false
    })
  },
  tabClick: function (e) {
    var that = this
    if (!that.data.tabone || !that.data.tabtwo || !that.data.tabthree || !that.data.tabfour) {
      that.setData({
        tabfixed: false
      })
    }
    if (e.target.dataset.id == 0) {
      that.setData({
        tabone: !that.data.tabone,
        tabtwo: false,
        tabthree: false,
        tabfour: false,
        tabfixed: true
      })
    } else if (e.target.dataset.id == 1) {
      that.setData({
        tabtwo: !that.data.tabtwo,
        tabone: false,
        tabthree: false,
        tabfour: false,
        tabfixed: true
      })
    } else if (e.target.dataset.id == 2) {
      that.setData({
        tabthree: !that.data.tabthree,
        tabone: false,
        tabtwo: false,
        tabfour: false,
        tabfixed: true
      })
    } else if (e.target.dataset.id == 3) {
      that.setData({
        tabfour: !that.data.tabfour,
        tabone: false,
        tabtwo: false,
        tabthree: false,
        tabfixed: true
      })
    }
    if (!that.data.tabone && !that.data.tabtwo && !that.data.tabthree && !that.data.tabfour ) {
      that.setData({
        tabfixed: false
      })
    }
    that.setData({
      tabNum: e.target.dataset.id
    })
  },

  search: function (e) {
    var search = e.detail.value;
    var that = this;
    that.setData({
      search: search
    });
    that.loadLists();
  },
  loadLists: function () {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Category/getLists',
      method: 'post',
      data: {
        search:that.data.search,
        bid:that.data.bid,
        gg:that.data.gg,
        hw:that.data.hw,
        jg:that.data.jg
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        var status = res.data.status;
        if (status == 1) {
          var lists = res.data.lists;
          
          that.setData({
            lists: res.data.lists
           
          });
          
        } else {
          that.setData({
            lists: res.data.lists

          });
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        };

      },
      fail: function () {
        that.setData({
          lists: []

        });
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  gotoProduct: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../product/product?productId=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    /* 获取定位地理位置 */
    // 新建bmap对象   
    var BMap = new bmap.BMapWX({
      ak: that.data.ak,
    });
    var fail = function (data) {
      console.log(data);
    };
    var success = function (data) {
      //返回数据内，已经包含经纬度  
      console.log(data);
      //使用wxMarkerData获取数据  
      wxMarkerData = data.originalData.result.addressComponent.city
      //把所有数据放在初始化data内  
      console.log(wxMarkerData)
      that.setData({
        address: wxMarkerData
      });
    }
    // 发起regeocoding检索请求   
    BMap.regeocoding({
      fail: fail,
      success: success
    });

    wx.request({
      url: app.d.ceshiUrl + '/Api/Category/index2',
      method: 'post',
      data: {
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        var status = res.data.status;
        if (status == 1) {
          var lists = res.data.lists;
          var brand = res.data.brand;
          var gueige = res.data.gueige;
          var huawen = res.data.huawen;
          that.setData({
            lists: lists,
            brand: brand,
            gueige: gueige,
            huawen: huawen
          });
          // that.countdown();
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        };

      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})